package com.oa.cms.service.impl;

import com.oa.cms.converter.CmsConfigureConverter;
import com.oa.cms.dao.CmsConfigureRepository;
import com.oa.cms.domain.dto.CmsConfigureDto;
import com.oa.cms.domain.entity.CmsConfigure;
import com.oa.cms.domain.enums.Status;
import com.oa.cms.query.CmsConfigureQuery;
import com.oa.cms.query.base.Page;
import com.oa.cms.service.CmsConfigureService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * Created by gold-jx on 2017/12/1.
 */
@Service
public class CmsConfigureServiceImpl implements CmsConfigureService{

    @Autowired
    private CmsConfigureRepository cmsConfigureRepository;

    @Autowired
    private CmsConfigureConverter cmsConfigureConverter;


    @Override
    public int save(CmsConfigure cmsConfigure) {
        if (StringUtils.isEmpty(cmsConfigure.getStatus())) {
            cmsConfigure.setStatus(Status.NORMAL);
        }
        cmsConfigure.setCreateTime(new Date());
        return StringUtils.isEmpty(cmsConfigureRepository.save(cmsConfigure)) ? 0 : 1;
    }

    @Override
    public int delete(Long id) {
        cmsConfigureRepository.delete(id);
        return 1;
    }

    @Override
    public int update(CmsConfigure cmsConfigure) {
        return StringUtils.isEmpty(cmsConfigureRepository.save(cmsConfigure)) ? 0 : 1;
    }

    @Override
    public Page<CmsConfigureDto> list(CmsConfigureQuery cmsConfigureQuery) {
        PageRequest pageable = new PageRequest(StringUtils.isEmpty(cmsConfigureQuery.getStart()) ? 0 : cmsConfigureQuery.getStart(),
                StringUtils.isEmpty(cmsConfigureQuery.getPageSize()) ? 10 : cmsConfigureQuery.getPageSize());

        org.springframework.data.domain.Page<CmsConfigure> page = cmsConfigureRepository.findAll(new Specification<CmsConfigure>() {
            @Override
            public Predicate toPredicate(Root<CmsConfigure> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmpty(cmsConfigureQuery.getConfigName())) {
                    list.add(criteriaBuilder.equal(root.get("configType").as(String.class), cmsConfigureQuery.getConfigType()));
                }
                if (!StringUtils.isEmpty(cmsConfigureQuery.getConfigType())) {
                    list.add(criteriaBuilder.equal(root.get("configName").as(String.class), cmsConfigureQuery.getConfigName()));
                }
                if (!StringUtils.isEmpty(cmsConfigureQuery.getStatus())) {
                    list.add(criteriaBuilder.equal(root.get("status").as(Status.class), cmsConfigureQuery.getStatus()));
                }
                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }
        }, pageable);

        int count = Integer.parseInt(page.getTotalElements() + "");

        List<CmsConfigureDto> cmsConfigureDtos = null;
        if (count > 0) {
            cmsConfigureDtos = cmsConfigureConverter.convertToDto(page.getContent());
        }

        if (CollectionUtils.isEmpty(cmsConfigureDtos)) {
            return new Page(count, cmsConfigureDtos);
        }
        return new Page<>(count, cmsConfigureDtos);
    }
}
